/*
 * Copyright (c) 2009 Roman Savelyev
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#ifndef __HOSTLINK_H__
#define __HOSTLINK_H__

#include <c32comm.h>
#include <e32base.h>

class CListenerCallBack : public CActive
    {
public:
    CListenerCallBack(const TCallBack& aCallBack, const TCallBack& aCancel);
    ~CListenerCallBack();
    TRequestStatus& Status();
    const TRequestStatus& Status() const;
    void SetActive();
private:
    void RunL();
    void DoCancel();
private:
    TCallBack iCallBack;
    TCallBack iCancel;
    };

class MHostLinkObserver
	{
public:
	virtual void LinkStatusChanged() = 0;
	virtual void HostError() = 0;
	virtual void DeviceErrorComplete(TInt aError) = 0;
	};

class CHostLink : public CBase
    {
public:
	enum TLinkStatus
		{
		ELinkNotReady = 0,
		ELinkReady = KSignalDTR
		};
    static CHostLink* NewL(MHostLinkObserver& aObserver);
    virtual ~CHostLink();
    TLinkStatus LinkStatus() const;
    void DeviceErrorL();
    TBool DeviceErrorInProgress() const;
    void WriteHost(const TDesC8& aBuffer, TRequestStatus& aStatus);
    void CancelWriteHost();
    void ReadHost(TDes8& aBuffer, TRequestStatus& aStatus);
    void CancelReadHost();
private:
    CHostLink(MHostLinkObserver& aObserver);
    void ConstructL();
    void GetSignalsAndNotification();
    void GetBreakNotification();
    static TInt LinkStatusCallBack(TAny* aThis);
    static TInt LinkStatusCancel(TAny* aThis);
    static TInt HostErrorCallBack(TAny* aThis);
    static TInt HostErrorCancel(TAny* aThis);
    static TInt DeviceErrorCallBack(TAny* aThis);
    static TInt DeviceErrorCancel(TAny* aThis);
private:
    MHostLinkObserver& iObserver;
    CListenerCallBack* iLinkStatusListener;
    CListenerCallBack* iHostErrorListener;
    CListenerCallBack* iDeviceErrorListener;
	TUint iSignals;
    RCommServ iServer;
    RComm iPort;
    };

#endif /*__HOSTLINK_H__*/
